<template>
	<view>
		<view class="img" style="text-align: center; height: 220px;">
			<image src="../../static/touxiang.png" mode="" style="width: 250px;height: 250px;scale: 0.4;"></image>
		</view>
		<view class="container">
		<uni-forms ref="form" :model="user" :rules="{}">
			<uni-forms-item label="用户名" name="name">

				<uni-easyinput v-model="user.name" placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item class="psw" label="密码" name="password">

				<uni-easyinput type="password" v-model="user.password" placeholder="请输入密码" />
			</uni-forms-item>
			<uni-forms-item label="" name="">
				<button type="primary" @click="login">登录</button> <!-- 设置click事件方法  -->
				<button type="primary" @click="goregister">没有账号,去注册</button>
			</uni-forms-item>
		</uni-forms>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				rules: {
					username: {
						rules: [{
							required: true,
							errorMessage: '请填写姓名'
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '请填写用户密码'
						},
						{
							minLength: 3,
							maxLength: 5,
							errorMessage: '密码长度在 {minLength} 到 {maxLength} 个字符',
						}
						]
					}
				},
				user: {
					username: '',
					password: ''
				},
				headicon: '',
				res:''
			}
		},
		methods: {
			
				goregister(){
					uni.navigateTo({
						url:"/pages/index/register"
					})
				},
			login(e) {
				uni.request({
					url: 'http://localhost:8090/freshmenapp/user/login',
					method: 'POST',
					data: this.user,
				success: res => {
						console.log(res);
						if (res.data.success === true) {
							let userStr = JSON.stringify(res.data.data)
							uni.setStorageSync('userInfo', userStr)
							this.headicon='../../static/tt.jpg'
							this.res=res.data.success
							uni.showToast({
								title: res.data.data.name + '登录成功',
								icon: "success",
							});
							uni.setStorageSync('userinfo',res.data.data)
							uni.switchTab({
								
								url:'../index/index'
							})
						} 
						else {
							uni.showToast({
								title: '用户名或密码错误',
								icon: 'error'
							});
										}
				
				
									},
									fail: (err) => {
										uni.showToast({
											title: '网络错误',
											icon: 'error'
										});
									},
									complete: () => {}
								});
							}
						},
						onLoad: () => {
							uni.onTabBarMidButtonTap(function(e) {
								console.log(e)
				
							})
		}
	
	}
</script>

<style>
.container {
	width: 90%;
		margin: auto !important;
		padding: 20rpx;
	}
	button{
		margin: 10px;
	}
</style>